const api = require("../api.js");
//获取应用实例
const {network, common} = getApp();

Page({
  data: {
    message: {
      Amount: 0,
      TotalAmount: 0,
      ExtensionNumber: 0,
      ExtensionAmount: 0,
      ShareAmount: 0,
      ShareNumber: 0,
      SaleAmount: 0,
      OrderNumber: 0
    }
  },
  // 去取现页面
  toApplyPage() {
    wx.navigateTo({url: `/pages/home/apply/index?amount=${this.data.message.Amount}`});
  },
  onLoad(e) {
    console.log(e, '赴俄无分文ufhe');
    e && common.bindTop(e.u);
  },
  onShow() {
    network.get(api.UserGetPartnerHomePage, {}).then((data) => {
      this.setData({
        message: data
      });
    });
  },
  // 推广员扫码
  PromoterToBindPoster() {
    wx.scanCode({
      success: (e) => {
        console.log(e, '扫描成功');
        const userMessage = JSON.parse(wx.getStorageSync('user'));
        let url = this.sliceString(decodeURIComponent(e.path || e.result || '?'));
        console.log(url);
        network.post(api.ScanningQRCode, {
          IsNewUser: userMessage.IsNewUser,
          PageUrl:  url,
          ScanningIdentity: 'promoters' // ['promoters' => 促销员, 'merchants' => 商家, 'consumer' =》 客户]
        }).then((data) => {
          console.log(data, '扫码成功结果');
          let success = false;
          let title = '';
          const result = data.QRCodeReturnString.toLocaleLowerCase();
          switch(result) {
            case 'qrcodeerror':
              title = '不支持的二维码';
              break;
            case 'alreadybound':
              title = '绑定失败，已绑定过';
              break;
            case 'bindsuccess':
              title = '绑定成功';
              success = true;
              break;
          }
          wx.showToast({
            title: title,
            icon: 'none'
          });
          console.log(data, "推广员");
        }, (e) => {
          wx.showToast({
            title: '绑定失败',
            icon: 'none',
            duration: 3000
          });
          console.log(e);
        })
      }
    });
  },
  MerchantsToBindPoster() {
    wx.scanCode({
      success: (e) => {
        const userMessage = JSON.parse(wx.getStorageSync('user'));
        console.log(e, '==============================');
        let url = this.sliceString(decodeURIComponent(e.path ||  e.result || '?'));
        console.log(url, '13246');
        network.post(api.ScanningQRCode, {
          IsNewUser: userMessage.IsNewUser,
          PageUrl: url, // e.path || e.result,
          ScanningIdentity: 'merchants' // ['promoters', 'merchants', 'consumer']
        }).then((data) => {
          console.log(data, '扫码成功结果');
          let success = false;
          let title = '';
          const result = data.QRCodeReturnString.toLocaleLowerCase();
          switch(result) {
            case 'qrcodeerror':
              title = '不支持的二维码';
              break;
            case 'alreadybound':
              title = '绑定失败，已绑定过';
              break;
            case 'bindsuccess':
              title = '绑定成功';
              success = true;
              break;
          }
          wx.showToast({
            title: title,
            icon: 'none'
          });
          console.log(data, "商家");
        }, () => {
          wx.showToast({
            title: '二维码已经被绑定或不是美好合伙人商城二维码',
            icon: 'none',
            duration: 3000
          });
        });
      }
    });
  },
  sliceString(url) {
    if (!url) {
      url = '?';
    }
    let index = url.indexOf('?');
    return url.slice(index, url.length);
  },
  toAwardFn(e) {
    console.log( e.currentTarget.id);
    switch (e.currentTarget.id) {
      case 'development':
        wx.navigateTo({url: `/pages/list/development-award/index?id=${e.currentTarget.id}`});
        break;
      case 'development-list':
        wx.navigateTo({url: `/pages/list/development-award-list/index?id=${e.currentTarget.id}`});
        console.log(1);
        break;
      case 'share':
        wx.navigateTo({url: `/pages/list/development-award/index?id=${e.currentTarget.id}`});
        console.log(2);
        break;
      case 'share-list':
        wx.navigateTo({url: `/pages/list/development-award-list/index?id=${e.currentTarget.id}`});
        console.log(3);
        break;
      case 'sale':
        wx.navigateTo({url: `/pages/list/development-award/index?id=${e.currentTarget.id}`});
        console.log(4);
        break;
      case 'sale-list':
        wx.navigateTo({url: `/pages/list/development-award-list/index?id=${e.currentTarget.id}`});
        console.log(5);
        break;
    }
    console.log(e);
  }
});
